import Base from '@/common/cmdBase/cmdBase';
import { EntityInfo, EntityPartType } from '@amcax/web-render';
import { FilletType } from '../type';

export default class CmdInteractor extends Base {
  setSourceIds: React.Dispatch<React.SetStateAction<string[]>>;
  exitFunc: () => void;
  type: string = FilletType.Fillet;
  radius: number = 0.05;
  private selectedData: string[] = [];
  constructor(
    setSourceIds: React.Dispatch<React.SetStateAction<string[]>>,
    exitFunc: () => void,
  ) {
    super();
    this.setSourceIds = setSourceIds;
    this.exitFunc = exitFunc;

    this.pickType = EntityPartType.line;
    this.registerEvents();
  }
  setType(type: string) {
    this.type = type;
  }
  setRadius(radius: number) {
    this.radius = radius;
  }
  onMouseLeft = (e: EntityInfo, selected: boolean) => {
    if (selected) {
      this.selectedData.push(Base.makeId(e));
      this.setSourceIds((prev) => {
        const ids = [...prev];
        ids.push(Base.makeId(e));
        return ids;
      });
    } else {
      // 取消选中
      this.setSourceIds((prev) => {
        const ids = [...prev];
        const index = ids.findIndex((id) => id === Base.makeId(e));
        if (index !== -1) {
          ids.splice(index, 1);
        }
        return ids;
      });
      this.selectedData = this.selectedData.filter(
        (id) => id !== Base.makeId(e),
      );
    }
  };
  onMouseMid = async () => {
    if (this.selectedData.length === 0) {
      return;
    }

    this.clear();
    this.reset();
  };
  reset() {
    this.selectedData = [];
    this.setSourceIds([]);
  }
  addExit(exitFunc: () => void) {
    this.exitFunc = exitFunc;
  }
  release(): void {}
  async handleApiCode() {}
  excute = async () => {
    await this.handleApiCode();
    this.reset();
    this.clear();
  };
}
